//
//  DownloadPopView.swift
//  ios_swift_demo1
//
//  Created by liutao on 2020/7/30.
//  Copyright © 2020 lt. All rights reserved.
//

import UIKit

class DownloadPopView: UIView {
    public var confirmBtnTouch: (() -> Void)?
    
    public var jumpWeChatBtnTouch: (() -> Void)?
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        sudokuConstraints()
    }
   
   required init?(coder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }
    
    //MARK: - 布局
      func sudokuConstraints(){
        let layerView = UIView()
        layerView.frame = CGRect(x: 0, y: 0, width: (300/375)*screenWidth, height: (180/375)*screenWidth)
        layerView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1)
        let layer = CALayer()
        layer.frame = layerView.bounds
        layer.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1).cgColor
        layerView.layer.addSublayer(layer)
        layerView.layer.cornerRadius = 10;
        layerView.layer.masksToBounds = true
        self.addSubview(layerView)
          
        addEnterButton(layerView)
        addPasterButton(layerView)
        addTitleLabel(layerView)
        addContentLabel(layerView)
     }
      
      func addEnterButton(_ view:UIView){
          //确定按钮
          let button1 = UIButton()
          button1.frame = CGRect(x: (18/375)*screenWidth, y: (124/375)*screenWidth, width: (125/375)*screenWidth, height: (40/375)*screenWidth)
          button1.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1)
          let layer1 = CALayer()
          layer1.frame = button1.bounds
          layer1.backgroundColor = UIColor(red: 0.92, green: 0.92, blue: 0.92, alpha: 1).cgColor
          button1.layer.addSublayer(layer1)
          button1.layer.cornerRadius = 20;
          button1.layer.masksToBounds = true
          button1.setTitle("确定", for: .normal)
          button1.setTitleColor(UIColor(red: 102, green: 102, blue: 102, alpha: 102), for: .normal)
          button1.titleLabel?.font = .systemFont(ofSize: 14)
          button1.addTarget(self, action: #selector(click1), for: .touchUpInside)
          view.addSubview(button1)
        
      }
    
    @objc func click1(){
        confirmBtnTouch?()
     }
    
    
      
      func  addPasterButton(_ view:UIView){
          let layerView = UIButton()
          layerView.frame = CGRect(x: (157/375)*screenWidth, y: (124/375)*screenWidth, width: (125/375)*screenWidth, height: (40/375)*screenWidth)
          // layerFillCode
          layerView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1)
          let layer = CALayer()
          layer.frame = layerView.bounds
          layer.backgroundColor = UIColor(red: 0.6, green: 0.6, blue: 0.6, alpha: 1).cgColor
          layerView.layer.addSublayer(layer)
          // gradientCode
          let gradient1 = CAGradientLayer()
          gradient1.colors = [UIColor(red: 0.67, green: 0.44, blue: 1, alpha: 1).cgColor, UIColor(red: 0.44, green: 0.33, blue: 1, alpha: 1).cgColor]
          gradient1.locations = [0, 1]
          gradient1.startPoint = CGPoint(x: 0.0, y: 0.5)
          gradient1.endPoint = CGPoint(x: 1.0, y: 0.5)
          gradient1.frame = layerView.bounds
          layerView.layer.addSublayer(gradient1)
          layerView.layer.cornerRadius = 20;
          layerView.layer.masksToBounds = true
          layerView.setTitle("去微信粘贴", for: .normal)
          layerView.setTitleColor(UIColor.white, for: .normal)
          layerView.titleLabel?.font = .systemFont(ofSize: 14)
        layerView.addTarget(self, action: #selector(click2), for: .touchUpInside)
          view.addSubview(layerView)
      }
    
    @objc func click2(){
        jumpWeChatBtnTouch?()
     }
    
    
     
    
    func addTitleLabel(_ view:UIView){
        let label = UILabel()
        let attrString = NSMutableAttributedString(string: "视频已保存至相册")
        label.frame = CGRect(x: (view.size.width-(150/375)*screenWidth)/2, y: (25/375)*screenWidth, width: (150/375)*screenWidth, height: 20)
        label.numberOfLines = 0
        label.textAlignment = .center
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont(name: "PingFang-SC-Medium", size: 17)!,.foregroundColor: UIColor(red: 0, green: 0, blue: 0,alpha:1)]
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        label.attributedText = attrString
        view.addSubview(label)
    }
    
    
    func addContentLabel(_ view:UIView){
        let label = UILabel()
        let attrString = NSMutableAttributedString(string: "下单文案已复制")
        label.frame = CGRect(x: (view.size.width-(150/375)*screenWidth)/2, y: (58/375)*screenWidth, width: (150/375)*screenWidth, height: 20)
        label.numberOfLines = 0
        label.textAlignment = .center
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont(name: "PingFang-SC-Medium", size: 14)!,.foregroundColor: UIColor(red: 0, green: 0, blue: 0,alpha:1)]
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        label.attributedText = attrString
        view.addSubview(label)
    }
}
